package com.zisu.aradvertising.service.impl;

import com.zisu.aradvertising.pojo.Advertiser;
import com.zisu.aradvertising.pojo.Cooperation;
import com.zisu.aradvertising.pojo.Sign;
import com.zisu.aradvertising.repository.DaoAdvertiser;
import com.zisu.aradvertising.repository.DaoCooperation;
import com.zisu.aradvertising.repository.DaoSign;
import com.zisu.aradvertising.service.AdvertiserService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdertiserServiceImpl implements AdvertiserService {
    //实例化抽象类
    @Resource
    private DaoAdvertiser daoAdvertiser;
    @Resource
    private DaoCooperation daoCooperation;
    @Resource
    private DaoSign daoSign;

    //实现账号密码登录业务逻辑
    @Override
    public Advertiser loginService(String aemail, String apassword) {
        Advertiser advertiser = daoAdvertiser.findAdvertiserByAemailAndApassword(aemail, apassword);
        return advertiser;
    }

    //实现注册业务逻辑
    @Override
    public Advertiser registService(Advertiser advertiser) {
        if (daoAdvertiser.findAdvertiserByAemail(advertiser.getAemail()) != null) {
            //已经有用户了，不可以注册
            return null;
        } else {
            Advertiser newadvertiser=daoAdvertiser.save(advertiser);
            advertiser.setApassword("");
            return newadvertiser;
        }
    }

    @Override
    //实现修改密码逻辑
    public Advertiser updatepasswordService(String aemail,String password){
        Advertiser advertiser = daoAdvertiser.findAdvertiserByAemail(aemail);
        if(advertiser !=null)
        {
            advertiser.setApassword(password);
            return daoAdvertiser.save(advertiser);
        }else
        {
            return null;
        }
    }

    @Override
    //实现返回所有广告商业务逻辑
    public List<Advertiser> findallService(){
        return daoAdvertiser.findAll();
    }
    @Override
    //返回一个广告位下所有广告商业务逻辑
    public  List<Advertiser> findallbyapidService(int apid){
        List<Cooperation> cooperationList=daoCooperation.findAllByApid(apid);
        List<Integer> integerList=new ArrayList<>();
        List<Sign> signList=new ArrayList<>();
        List<Integer> integerList1=new ArrayList<>();
        List<Advertiser> advertiserList=new ArrayList<>();
        for(Cooperation x:cooperationList){
            System.out.println(x);
            integerList.add(x.getCoid());
        }
        for(Integer x1:integerList){
            System.out.println(x1);
            signList.add(daoSign.findByCoid(x1));
        }
        for(Sign x2:signList){
            System.out.println(x2);
            integerList1.add(x2.getAid());
        }
        for(Integer x3:integerList1){
            System.out.println(x3);
            advertiserList.add(daoAdvertiser.findByAid(x3));
        }
        if(advertiserList.size()!=0)
        return advertiserList;
        else {return null;}
    }
    @Override
    //实现根据合作号找到广告商业务逻辑
    public Advertiser findbycoidService(int coid){
       Sign sign=daoSign.findByCoid(coid);
       if(sign==null){return null;}
        return daoAdvertiser.findByAid(sign.getAid());
    }
    //实现根据广告商的id找到广告商业务逻辑
    public Advertiser findbyaidService(int aid){
        Advertiser advertiser=daoAdvertiser.findByAid(aid);
        return advertiser;
    }
}
